import mysql from 'mysql2/promise'

const pool = mysql.createPool({
  host: '127.0.0.1',
  port: 3306,
  user: 'root',
  password: '123456',
  database: 'music-min',
  connectionLimit: 10
})

async function testHistory() {
  try {
    // 检查表是否存在
    const [tables] = await pool.query("SHOW TABLES LIKE 'play_history'")
    console.log('play_history表存在:', tables.length > 0)
    
    if (tables.length === 0) {
      console.log('创建play_history表...')
      await pool.query(`
        CREATE TABLE play_history (
          id int(11) NOT NULL AUTO_INCREMENT,
          user_id int(11) NOT NULL,
          music_id int(11) NOT NULL,
          play_duration int(11) DEFAULT 0,
          play_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (id),
          KEY idx_user_id (user_id),
          KEY idx_music_id (music_id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
      `)
      console.log('play_history表创建成功')
    }
    
    // 查看现有数据
    const [historyData] = await pool.query('SELECT * FROM play_history ORDER BY play_time DESC LIMIT 10')
    console.log('播放历史数据:')
    console.table(historyData)
    
    // 查看用户数据
    const [userData] = await pool.query('SELECT id, username, nickname FROM users LIMIT 5')
    console.log('用户数据:')
    console.table(userData)
    
    // 查看音乐数据
    const [musicData] = await pool.query('SELECT id, title, artist FROM music LIMIT 5')
    console.log('音乐数据:')
    console.table(musicData)
    
  } catch (error) {
    console.error('测试失败:', error)
  } finally {
    await pool.end()
  }
}

testHistory()

